package com.junxonline.instant.admin.controller.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.junxonline.instant.dao.entity.model.config.Role;
import com.junxonline.instant.dao.entity.vo.common.IdsVO;
import com.junxonline.instant.dao.entity.vo.config.PermissionVO;
import com.junxonline.instant.dao.entity.vo.config.RoleVO;
import com.junxonline.instant.admin.service.config.MenuService;
import com.junxonline.instant.admin.service.config.OrganizationService;
import com.junxonline.instant.admin.service.config.RoleService;
import com.junxonline.instant.common.annotation.OperationLog;
import com.junxonline.instant.common.entity.common.CommonReqPage;
import com.junxonline.instant.common.entity.common.CommonResult;
import com.junxonline.instant.common.enumeration.AdminErrorEnum;
import com.junxonline.instant.common.enumeration.OperationEnum;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 角色管理Controller
 *
 * @author JunX
 * @date 2021-07-04
 */
@Log4j2
@RestController
@RequestMapping(value = "/config/role")
public class RoleController {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    @Autowired
    private OrganizationService organizationService;

    /**
     * 查询组织机构树（TreeSelect）
     *
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:role:query")
    @OperationLog(label = "角色管理-查询组织机构树", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryOrganizationTree")
    public CommonResult queryOrganizationTree() {
        return CommonResult.success(organizationService.queryOrganizationTree(new QueryWrapper<>()));
    }

    /**
     * 查询菜单权限树（Tree）
     *
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:role:query")
    @OperationLog(label = "角色管理-查询菜单权限树", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryPermissionTree")
    public CommonResult queryPermissionTree() {
        String token = request.getHeader("Authorization");
        return CommonResult.success(menuService.queryPermissionTree(token));
    }

    /**
     * 查询数据权限树（Tree）
     *
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:role:query")
    @OperationLog(label = "角色管理-查询数据权限树", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryDataPermissionTree")
    public CommonResult queryDataPermissionTree() {
        return CommonResult.success(organizationService.queryOrganizationTree(new QueryWrapper<>()));
    }

    /**
     * 查询角色列表
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:role:query")
    @OperationLog(label = "角色管理-查询列表", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryList")
    public CommonResult queryList(@RequestBody(required = false) CommonReqPage<RoleVO> vo) {
        return CommonResult.success(roleService.queryList(vo, new QueryWrapper<>()));
    }

    /**
     * 查询角色
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:role:query")
    @OperationLog(label = "角色管理-查询角色", type = OperationEnum.QUERY)
    @PostMapping(value = "/query")
    public CommonResult query(@RequestBody(required = false) RoleVO vo) {
        if (vo.getId() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        return CommonResult.success(roleService.query(vo.getId()));
    }

    /**
     * 新增角色
     *
     * @param role 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:role:add")
    @OperationLog(label = "角色管理-新增角色", type = OperationEnum.ADD)
    @PostMapping(value = "/add")
    public CommonResult add(@RequestBody(required = false) Role role) {
        if (role == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        roleService.add(role);
        return CommonResult.success();
    }

    /**
     * 编辑角色
     *
     * @param role 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:role:edit")
    @OperationLog(label = "角色管理-编辑角色", type = OperationEnum.EDIT)
    @PostMapping(value = "/edit")
    public CommonResult edit(@RequestBody(required = false) Role role) {
        if (role == null || role.getId() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        if (role.getId() == 1 && role.getStatus() == 0) {
            // TODO 处理超级管理员特殊逻辑
            return CommonResult.fail(AdminErrorEnum.USER_ADMIN_ROLE_NOT_DISABLE);
        }
        roleService.edit(role);
        return CommonResult.success();
    }

    /**
     * 删除角色
     *
     * @param idsVO 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:role:delete")
    @OperationLog(label = "角色管理-删除角色", type = OperationEnum.DELETE)
    @PostMapping(value = "/delete")
    public CommonResult delete(@RequestBody(required = false) IdsVO<List<Long>> idsVO) {
        if (idsVO == null || idsVO.getIds() == null || idsVO.getIds().size() <= 0) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        for (Long id : idsVO.getIds()) {
            // TODO 处理超级管理员特殊逻辑
            if (id == 1) {
                return CommonResult.fail(AdminErrorEnum.USER_ADMIN_ROLE_NOT_DELETE);
            }
        }
        roleService.delete(idsVO.getIds());
        return CommonResult.success();
    }

    /**
     * 查询角色权限
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:role:query")
    @OperationLog(label = "角色管理-查询角色权限", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryRolePermission")
    public CommonResult queryRolePermission(@RequestBody(required = false) RoleVO vo) {
        if (vo.getId() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        return CommonResult.success(roleService.queryRolePermission(vo));
    }

    /**
     * 设置权限
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:role:permission")
    @OperationLog(label = "角色管理-设置权限", type = OperationEnum.EDIT)
    @PostMapping(value = "/setPermission")
    public CommonResult setPermission(@RequestBody(required = false) PermissionVO vo) {
        if (vo == null || vo.getId() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        roleService.setPermission(vo);
        return CommonResult.success();
    }

}
